package main.java.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import main.java.model.Handle;
import main.java.view.BlocView;
import main.java.view.MainFrame;

public class Controller implements ActionListener {
    
    private MainFrame mainFrame;
    private Handle handle;
    
    /**
     * Constructor Controller
     * @param mainframe : the view
     * @param handle : the handle
     */
    public Controller(MainFrame mainframe, Handle handle) {
        this.mainFrame = mainframe;
        this.handle = handle;
    }
    
    /**
     * Action performed depending on the event
     * @param ActionEvent : the event
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        //If handle button up
        if(arg0.getSource() == mainFrame.getPilotMainView().getButtonsView().getHandleButtonUp()) { 
        	if (!mainFrame.getPilotMainView().getButtonsView().getHandleButtonUp().getText().equals("Manually")) {
	            //Disable the button up
	            mainFrame.getPilotMainView().getButtonsView().getHandleButtonUp().setEnabled(false); 
	            //Change the image
	            mainFrame.getPilotMainView().getHandleView().switchImage(); 
	            //Handle goes down
	            handle.goDown(); 
        	} else {
        		//For each BlocView open manually each door and gear
        		for (BlocView bloc : mainFrame.getBlocView()) {
        			bloc.getDoorsView().switchImage("Opened");
        			bloc.getGearsView().switchImage("Down");
        		}
        		//Set lights to green and disable button
        		mainFrame.getPilotMainView().getLightsView().switchImage("Green");
        		mainFrame.getPilotMainView().getButtonsView().getHandleButtonUp().setEnabled(false);
        	}
        } else {
            //If handle button down
            if(arg0.getSource() == mainFrame.getPilotMainView().getButtonsView().getHandleButtonDown()) { 
                //Disable the button down
                mainFrame.getPilotMainView().getButtonsView().getHandleButtonDown().setEnabled(false); 
                //Change the image
                mainFrame.getPilotMainView().getHandleView().switchImage(); 
                //Handle goes up
                handle.goUp(); 
            }
        }
    }
}
